package Users;


import Questions.Question;
import TestApp.IOSystem;
import TestApp.MyNonNumberException;
import TestApp.Test;

import java.util.ArrayList;

/**
 * @author Myhaylo Kotsyuruba
 * @version v.1.4   14.03.13
 */

public class User {
    private String name;
    private ArrayList<TestReport> testReports;

    public User(String name) {
        this.name = name;
        testReports = new ArrayList<TestReport>();
    }

    public String getName() {
        return name;
    }

    public void getTested(Test test) {
        TestReport testReport = new TestReport(test.toString());
        testReports.add(testReport);
        IOSystem.show(test.toString());
        for (int index = 0; index < test.getQuantityQuestions(); index++) {
            AnswerReport answerReport = handleQuestion(test,index);
            testReport.addAnswerReport(answerReport);
        }
        IOSystem.show(testReport.toString());
    }

    private AnswerReport handleQuestion(Test test, int index) {
        test.showQuestion(index);
        String userAnswer = IOSystem.getAnswer();

        try {
            return test.createAnswerReport(index, userAnswer);
        } catch (MyNonNumberException e) {
            IOSystem.show("\nWrong format of Answer. Ty again!!!\n");
            return handleQuestion(test, index);
        }
    }

    public void showAllResults() {
        IOSystem.show(getAllTestReport());
    }

    public String getAllTestReport() {
        if (testReports.size() == 0) {
            return "You have not yet tested!!!";
        }
        String result = "";
        for (TestReport currentTest : testReports) {
            result =result.concat(currentTest.toString() + "\n");
        }
        return result;
    }
}
